package com.example.imoocmusicdemo.utils;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.Toast;

import com.blankj.utilcode.util.RegexUtils;
import com.example.imoocmusicdemo.activitys.BaseActivity;
import com.example.imoocmusicdemo.activitys.LoginActivity;
import com.example.imoocmusicdemo.activitys.MainActivity;
import com.example.imoocmusicdemo.helps.DBOpenHelper;

import java.util.ArrayList;

public class UserUtils extends BaseActivity {


    public static boolean validateLogin(Context context, String phone, String password,SharedPreferences.Editor editor,CheckBox cbRmbPsw,DBOpenHelper mDBOpenHelper,String userName) {


//        简单的
//        RegexUtils.isMobileSimple(phone);
//        精确的
        if (!RegexUtils.isMobileExact(phone)) {

            Toast.makeText(context, "无效手机号码（手机号码注册过了吗？）", Toast.LENGTH_SHORT).show();
            if (TextUtils.isEmpty(phone)) {
                Toast.makeText(context, "没有输入手机号码", Toast.LENGTH_SHORT).show();
                return false;
            }
        }else if (TextUtils.isEmpty(password)) {
            Toast.makeText(context, "请输入手机密码", Toast.LENGTH_SHORT).show();
            return false;
        } else {
            ArrayList<User> data = mDBOpenHelper.getAllData();
            boolean match = false;
            boolean match2 = false;
            for (int i = 0; i < data.size(); i++) {
                User user = data.get(i);
                if (phone.equals(user.getName()) && password.equals(user.getPassword())) {
                    userName = user.getName();
                    match = true;
                    if(cbRmbPsw.isChecked()){
                        editor.putBoolean("flag",true);
                        editor.putString("user",user.getName());
                        editor.putString("psw",user.getPassword());
                        editor.apply();
                        match2 = true;
                    }else {
                        editor.putString("user",user.getName());
                        editor.putString("psw","");
                        editor.clear();
                        editor.apply();
                        match2 = false;
                    }
                    break;
                } else {
                    match = false;
                }
            }
            if (match) {
                Toast.makeText(context, "登录成功", Toast.LENGTH_SHORT).show();
                Runnable target;
            } else {
                Toast.makeText(context, "用户名或密码不正确，请重新输入", Toast.LENGTH_SHORT).show();
                return false;
            }
            return true;
        }




        return false;
    }
}

